//
//  FKOrderVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/22.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKOrderVC.h"
#import <JXCategoryView.h>
#import "FKOrderListVC.h"
#import "FKWeakTimer.h"

@interface FKOrderVC ()<JXCategoryViewDelegate>

@property (nonatomic, weak) JXCategoryTitleView *categoryView;

@end

@implementation FKOrderVC
{
    NSTimer *_timer;
}

#pragma mark - private
- (void)startTimer
{
    [self removeTimer];
    _timer = [FKWeakTimer fk_scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:true];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    [_timer fire];
}

- (void)updateTime
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateReserveTime" object:nil];
}

- (void)removeTimer
{
    if (_timer) [_timer invalidate];
    _timer = nil;
}

- (void)dealloc
{
    [self removeTimer];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([self.type isEqualToString:@"1"]) {
        self.title = @"核销订单";
    } else {
        self.title = @"物流订单";
    }
    
    NSArray *titles = @[@"全部", @"待付款", @"待消费", @"已完成", @"售后"];
    NSArray *statuses = @[@"", @"10", @"20", @"40", @"50"];
    if ([self.type isEqualToString:@"2"]) {
        titles = @[@"全部", @"待付款", @"待发货", @"待收货", @"售后"];
        statuses = @[@"", @"10", @"20", @"30", @"50"];
    }
    
    // 默认为user类型
    CGFloat headerH = 44;
    JXCategoryTitleView *categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, headerH)];
    [self.view addSubview:categoryView];
    
    UIView *separator = [UIView fk_lineWithFrame:CGRectMake(0, headerH - 0.8, fkScreenW, 0.8)];
    [self.view addSubview:separator];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    scrollView.pagingEnabled = true;
    scrollView.showsHorizontalScrollIndicator = false;
    scrollView.bounces = false;
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(separator.mas_bottom);
        make.left.bottom.right.offset(0);
    }];
    
    self.categoryView = categoryView;
    categoryView.titleSelectedColor = fkMainColor;
    categoryView.delegate = self;
    categoryView.titleColorGradientEnabled = YES;
    categoryView.titles = titles;
    categoryView.backgroundColor = [UIColor whiteColor];
    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    categoryView.indicators = @[lineView];
    lineView.indicatorLineViewColor = fkMainColor;
    
    categoryView.contentScrollView = scrollView;
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag | UIScrollViewKeyboardDismissModeInteractive;
    
    CGFloat scrollH = fkScreenH - fkStatusAndNaBarH - headerH;
    for (NSInteger index = 0; index < titles.count; index++) {
        FKOrderListVC *vc = [[FKOrderListVC alloc] init];
        [self addChildViewController:vc];
        vc.status = statuses[index];
        vc.type = self.type;
        vc.view.frame = CGRectMake(fkScreenW * index, 0, fkScreenW, scrollH);
        [scrollView addSubview:vc.view];
        
        // 让第一个控制器加载数据
        if ([self.status isEqualToString:vc.status]) {
            [vc refreshData];
            categoryView.defaultSelectedIndex = index;
            [vc refreshData];
        } else {
            if (index == 0) [vc refreshData];
        }
    }
    scrollView.contentSize = CGSizeMake(fkScreenW * titles.count, 0);
    
    // 开启定时器
    [self startTimer];
}


#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index
{
    FKOrderListVC *vc = self.childViewControllers[index];
    [vc refreshData];
}

@end
